package com.scaffold.authority;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import wangjg.security.core.ResourceFactory;

import com.scaffold.authority.entity.Resource;
import com.scaffold.authority.service.ResourceService;

@Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class DbResourceFactory implements ResourceFactory, InitializingBean{
	
	@Autowired
	private ResourceService service;
	
	private Set<Resource> resources = new LinkedHashSet<Resource>();
	
	public Iterator<Resource> iterator() {
		return this.resources.iterator();
	}

	public void reload(){
		List<Resource> list = service.findAll();
		this.resources.clear();
		this.resources.addAll(list);
	}

	public ResourceService getService() {
		return service;
	}

	public void setService(ResourceService service) {
		this.service = service;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.reload();
	}
	
}
